package cn.edu.nyist.integration.config;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {

    private int serverPort;

    public  String getUrl(){
        InetAddress inetAddress = null;
        try{
            inetAddress = InetAddress.getLocalHost();
        }catch (UnknownHostException e){
            e.printStackTrace();
        }

        return "http://"+inetAddress.getHostAddress() +":"+this.serverPort;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent webServerInitializedEvent) {
        this.serverPort = webServerInitializedEvent.getWebServer().getPort();
    }
}
